<?php
namespace app\utils;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class JwtService
{


    private static $key = 'your-secret-key';

    public function _construct()
    {
        self::$key = env('JWT_KEY');

    }

    // 生成 Token
    public static function createToken(array $payload, int $exp = 7200000): string
    {
        $payload['iat'] = time();
        $payload['exp'] = time() + $exp;

        return JWT::encode($payload, self::$key, 'HS256');
    }

    // 验证 Token
    public static function verifyToken(string $token): array
    {
        try {
            return (array) JWT::decode($token, new Key(self::$key, 'HS256'));
        } catch (\Exception $e) {
            throw new \Exception('Token 无效或已过期');
        }
    }
}
